package com.njwb.day15.zqzb.method;

import java.lang.reflect.Field;
import java.lang.reflect.InvocationTargetException;
import java.lang.reflect.Method;

public class MethodDemo03 {

    public static void main(String[] args) {
            Class cls = MyMath.class ;


        try {
            Method addMethod = cls.getDeclaredMethod("add",int.class,int .class) ;
            addMethod.setAccessible(true);
            Object returnValue = addMethod.invoke(cls,6,2);
            System.out.println("结果为："+returnValue);

            Method oddMethod = cls.getDeclaredMethod("isOdd", int.class);
            Boolean flag = (Boolean) oddMethod.invoke(null,8);
            System.out.println("flag："+flag);

            Method setValueMethod = cls.getDeclaredMethod("setValue", String.class);
            MyMath m = new MyMath();
            setValueMethod.invoke(m,"helloworld");
            Field field = cls.getDeclaredField("value");
            field.setAccessible(true);
            System.out.println(field.get(m));
        } catch (NoSuchMethodException e) {
            e.printStackTrace();
        } catch (InvocationTargetException e) {
            e.printStackTrace();
        } catch (IllegalAccessException e) {
            e.printStackTrace();
        } catch (NoSuchFieldException e) {
            e.printStackTrace();
        }

    }

}
